package cn.liit.weixin.userinfo.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.alibaba.fastjson.JSONObject;

import cn.liit.weixin.core.utils.EmojiFilter;
import cn.liit.weixin.userinfo.model.Group;
import cn.liit.weixin.userinfo.model.UserInfo;
import name.zbq.bush.utils.http.HttpConnect;

/**
 * 获取用户基本信息
 * @author zhangboqi
 *
 */
public class Util {
	public final static String APIURL = "https://api.weixin.qq.com/cgi-bin/user/info?";
	public final static String APIURL_BATCHGET = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?";
	public final static String GROUP = "https://api.weixin.qq.com/cgi-bin/groups/get?";
	/**
	 * 获取用户信息
	 * @param accessToken 
	 * @param openid 
	 * @return 如果获取失败，返回null 如果用户未关注只返回 关注状态subscribe和openid和unionid
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static UserInfo getUserInfo(String accessToken,String openid){
		Map<String,Object> result = HttpConnect.getInstance().doGetJson(
				APIURL+"access_token="+accessToken+"&openid="+openid+"&lang=zh_CN",
				null, 
				"UTF-8");
		Object errcode = result.get("errcode");
		if(errcode == null){
			UserInfo userinfo = new UserInfo();
			try {
				BeanUtils.copyProperties(userinfo, result);
			} catch (IllegalAccessException | InvocationTargetException e) {
				e.printStackTrace();
			}
			return userinfo;
		}
		return null;
	}
	
	/**
	 * 批量获取用户信息
	 * @param accessToken
	 * @param openids openid集合
	 * @return
	 */
	public static List<UserInfo> getUserInfo(String accessToken,List<String> openids){
		if(openids == null || openids.size() == 0){
			return null;
		}
		StringBuffer paramStr = new StringBuffer("{\"user_list\": [");
		for(int i = 0;i < openids.size(); i++){
			paramStr.append("{"+
            "\"openid\": \""+openids.get(i)+"\"," +
            "\"lang\": \"zh_CN\""+
        	"}");
			if(i+1 != openids.size()){
				paramStr.append(",");
			}
		}
		paramStr.append("]}");
		Map result = HttpConnect.getInstance().doPostJson(APIURL_BATCHGET+"access_token="+accessToken+"&lang=zh_CN", paramStr.toString());
		Object errcode = result.get("errcode");
		if(errcode == null){
			return  JSONObject.parseArray(result.get("user_info_list").toString(), UserInfo.class);
		}
		return null;
	}
	
	/**
	 * 批量获取用户信息
	 * @param accessToken
	 * @param openids openid数组
	 * @return
	 */
	public static List<UserInfo> getUserInfo(String accessToken,String[] openids){
		return getUserInfo(accessToken, Arrays.asList(openids));
	}
	/**
	 * 获取分组信息
	 * @param accessToken 
	 * @return 如果获取失败，返回null
	 */
	public static List<Group> getWeixinGroup(String accessToken,String openid){
		Map<String,Object> result = HttpConnect.getInstance().doGetJson(GROUP+"access_token="+accessToken+"&lang=zh_CN",null,"UTF-8");
		Object errcode = result.get("errcode");
		if(errcode == null){
			return JSONObject.parseArray(result.get("groups").toString(), Group.class);
		}
		return null;
	}
	public static void main(String[] args) throws Exception {
		/*UserInfo userinfo = getUserInfo("ENDt3UdHph6huZCHQFAtRyTe2iPqVsZRg2TGhlpDlIUxIfJ0-Imv4FVibN0eQs_qBmGtGbGGBfy_POXpzwG2U6KrqfXyMFoKo8stvu6b7QsBOWfAFATGO",
				"okyoyuCm9m75YG2dzhO7ZeDZDsP4");
		System.out.println(userinfo);*/
		/*List<UserInfo> list = getUserInfo("ENDt3UdHph6huZCHQFAtRyTe2iPqVsZRg2TGhlpDlIUxIfJ0-Imv4FVibN0eQs_qBmGtGbGGBfy_POXpzwG2U6KrqfXyMFoKo8stvu6b7QsBOWfAFATGO", 
				new String[]{"okyoyuHLHcY5z1JCq1guLBbQO4JI","okyoyuCm9m75YG2dzhO7ZeDZDsP4"});*/
		/*UserInfo usrinfo = Util.getUserInfo("b5u8Yx28na_dlP8yKozu6iznsFxyBx_fuu8OP4NS9-6tLYPtXz4ZICm_UPeMgofa1MiqucClZcHRl-_ON8yxzdU0Eo0wjHkGrKYEZN-Oh9a5FhtD8cN0wDds2Q4dhTDkDHOcADALPW",
				"o_AT_t6qXiFixsp0l4p3ALhvZN_M");
				//o_AT_t6qXiFixsp0l4p3ALhvZN_M o_AT_t-Ce79H6KkZxCEu1vpJ3hOI
		System.out.println(EmojiFilter.filterEmoji2(usrinfo.getNickname()));*/
		/*System.out.println(list);*/
		List<Group> gpList=getWeixinGroup("Fd7Uw_k3-hx2UXDe3FblxkWWT2_qRxlX1BztLBsPRJw2Tm4jZ3IBSvg_iOsu9CK1Cd9_59CLmkmcKkoLj54JhzKfb30qIT-xV0zzeABcO3yWK0mlXdOCu10lvn0HyoT0ACChAAAFHH", null);
		System.out.println(gpList);
	}
}
